package com.base.taskreports.reports.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.taskreports.common.utlis.DateUtils;
import com.base.taskreports.common.utlis.ThresholdUtlis;
import com.base.taskreports.demoExel.entity.Indicator;
import com.base.taskreports.reports.dao.master.ExcelBeanDao;
import com.base.taskreports.reports.entiyt.master.ExcelBean;
import com.base.taskreports.reports.entiyt.master.TThresholdEntity;
import com.base.taskreports.reports.service.ExcelBeanService;
import com.base.taskreports.reports.service.TThresholdService;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张玉涛
 * @since 2021-11-09
 */
@DS("master")
@Service("excelBeanService")
public class ExcelBeanServiceImpl extends ServiceImpl<ExcelBeanDao, ExcelBean> implements ExcelBeanService {

	public static void main(String[] args) {
		//Indicator.LJ_M001
		//List<Indicator> list =Arrays.stream(Indicator.values()).filter(e -> e.getName().equals("PQL_M001")).collect(Collectors.toList()) ;
		BigDecimal t1 = new BigDecimal("88") ;
		BigDecimal t2 = new BigDecimal("10000") ; ;
		BigDecimal subtract = t1.subtract(t2);
		BigDecimal floatValue = subtract.divide(t2,4,BigDecimal.ROUND_HALF_UP);
		System.out.println(floatValue);
	}

	@Autowired
	private TThresholdService tThresholdService ;

	@Override
	public List<ExcelBean> getIndicators08(String strDate,String indicatorCode) throws ParseException {
		return baseMapper.getIndicators08(DateUtils.previousDay(strDate,-7)
				,DateUtils.previousDay(strDate,-6)
				,DateUtils.previousDay(strDate,-5)
				,DateUtils.previousDay(strDate,-4)
				,DateUtils.previousDay(strDate,-3)
				,DateUtils.previousDay(strDate,-2)
				,DateUtils.previousDay(strDate,-1)
				,strDate
				,indicatorCode
		) ;
	}

	@Override
	public List<ExcelBean> getIndicators03(String strDate,String indicatorCode) throws ParseException {
		return baseMapper.getIndicators03(DateUtils.previousDay(strDate,-2)
				,DateUtils.previousDay(strDate,-1)
				,strDate
				,indicatorCode
		) ;
	}

	/**
	 * 获取Excel输出结果集
	 * @param strDate			"2021-11-11"
	 * @param indicatorCode		"PQL_M001"
	 * @return
	 * @throws ParseException
	 */
	public List<List<Object>> getExcelList(String strDate,String indicatorCode) throws ParseException{

		List<List<Object>> excelList = null ;

		List<ExcelBean> list = null ;

		if(Indicator.valueOf(indicatorCode).getDayNum() == -3){

			list = this.getIndicators03(strDate,indicatorCode) ;

		}

		if(Indicator.valueOf(indicatorCode).getDayNum() == -8){

			list = this.getIndicators08(strDate,indicatorCode) ;

		}

		QueryWrapper<TThresholdEntity> wrapper = new QueryWrapper() ;
		wrapper.eq("indicator_code",indicatorCode) ;

		// 获取阈值信息
		TThresholdEntity tThresholdEntity = tThresholdService.getOne(wrapper) ;

		if(list != null && list.size()>0 && tThresholdEntity != null && tThresholdEntity.getThresholdValue()!=null ){

			excelList = new ArrayList<List<Object>>() ;

			String expression = tThresholdEntity.getThresholdValue() ;

			for(ExcelBean excelBean : list){

				BigDecimal t1 = null ;

				// 是否计算浮动值
				if(Indicator.valueOf(indicatorCode).getFloatFlag()){

					BigDecimal floatValue = null ;

					BigDecimal t2 = null ;

					if(excelBean.getT1() != null) {
						t1 = new BigDecimal(excelBean.getT1());
					}else {
						t1 = BigDecimal.ZERO ;
					}

					if(excelBean.getT2() != null){
						t2 = new BigDecimal(excelBean.getT2()) ;
					}

					if(t2 == null && excelBean.getT3() != null){
						t2 = new BigDecimal(excelBean.getT3()) ;
					}

					if(t2 == null && excelBean.getT4() != null){
						t2 = new BigDecimal(excelBean.getT4()) ;
					}

					if(t2 == null && excelBean.getT5() != null){
						t2 = new BigDecimal(excelBean.getT5()) ;
					}

					if(t2 == null && excelBean.getT6() != null){
						t2 = new BigDecimal(excelBean.getT6()) ;
					}

					if(t2 == null && excelBean.getT7() != null){
						t2 = new BigDecimal(excelBean.getT7()) ;
					}

					if(t2 == null && excelBean.getT8() != null){
						t2 = new BigDecimal(excelBean.getT8()) ;
					}

					if( t2 != null && t2.compareTo(BigDecimal.ZERO) != 0 ){
						BigDecimal subtract = t1.subtract(t2);
						floatValue = subtract.divide(t2,4,BigDecimal.ROUND_HALF_UP);
					}

					//Boolean flag = threshold(expression,floatValue) ;

					List<Object> objList = this.getModel(excelBean,indicatorCode,expression,floatValue);

					excelList.add(objList) ;

				}else{
					List<Object> objList = this.getModel(excelBean,indicatorCode,expression,null);
					excelList.add(objList) ;
				}
			}
		}

		return excelList ;
	}

	/**
	 *
	 * @param excelBean
	 * @param indicatorCode		"PQL_M001"
	 * @param expression
	 * @param floatValue
	 * @return
	 */
	public List<Object> getModel(ExcelBean excelBean,String indicatorCode,String expression,BigDecimal floatValue){

		Indicator indicator = Indicator.valueOf(indicatorCode) ;

		List<Object> objList = new ArrayList<>() ;

		if(indicator.getStartCell()==1){
			objList.add(excelBean.getParameter1()==null?"":excelBean.getParameter1()) ;
		}

		if(indicator.getStartCell()==2){
			objList.add(excelBean.getParameter1()==null?"":excelBean.getParameter1()) ;
			objList.add(excelBean.getParameter3()==null?"":excelBean.getParameter3()) ;
		}

		if(indicator.getDayNum() == -8){
			objList.add(excelBean.getT8()==null?0:excelBean.getT8()) ;
			objList.add(excelBean.getT7()==null?0:excelBean.getT7()) ;
			objList.add(excelBean.getT6()==null?0:excelBean.getT6()) ;
			objList.add(excelBean.getT5()==null?0:excelBean.getT5()) ;
			objList.add(excelBean.getT4()==null?0:excelBean.getT4()) ;
		}

		objList.add(excelBean.getT3()==null?0:excelBean.getT3()) ;
		objList.add(excelBean.getT2()==null?0:excelBean.getT2()) ;
		objList.add(excelBean.getT1()==null?0:excelBean.getT1()) ;

		Boolean flag = null ;

		if(indicator.getFloatFlag()){

			if(floatValue != null){
				objList.add(floatValue) ;
			}else{
				objList.add(0) ;
				floatValue = BigDecimal.valueOf(0) ;
			}

			flag = threshold(expression,floatValue) ;

		}else{
			BigDecimal t1 = null ;

			if(excelBean.getT1()!=null){
				t1 = new BigDecimal(excelBean.getT1()) ;
			}else{
				t1 = BigDecimal.valueOf(0) ;
			}

			flag = threshold(expression,t1 ) ;
		}

		objList.add(ThresholdUtlis.setThresholdValue(expression)) ;
		objList.add(flag?"未超限":"已超限") ;

		return objList ;
	}

	/**
	 * 阈值判断
	 * @param expression
	 * @param value
	 * @return
	 */
	public Boolean threshold(String expression,Object value){

		Expression exp =  AviatorEvaluator.compile(expression);

		Map<String,Object> map1 = new HashMap<String,Object>();
		map1.put("Value", value);
		Boolean bool = (Boolean) exp.execute(map1);

		return bool ;
	}
}
